// pages/takecash/takecash.js
const app = getApp();
import { $wuxKeyBoard } from '../../../../components/wux'   //引入安全键盘
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankData: null,  //页面设置
    bankCardList: null,  //银行卡列表
    money: 0, //提现金额
    isSetPass: null //是否设置过支付密码，0未设置，1已设置
  },

  //选择银行卡
  selectBankCard: function(){
    wx.navigateTo({
      url: '../bankCardList/bankCardList?select=true',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: app.baseURL + 'Distribution&a=show',
      data: {
        user_id: app.globalData.userId
      },
      success: res => {
        this.setData({
          bankData: res.data.map,
          bankCardList: res.data.list,
          isSetPass: res.data.map.is_pwd //判断支付密码是否设置
        })
      },
      fail: function (res) { }
    })
  },
  //监听表单提交
  submit(e){
    wx.showLoading({title: '请稍后...',});
    if (!this.data.bankCardList) {
      wx.showToast({
        title: '请添加银行卡！',
        icon: 'none'
      });
      return false
    }
    if (!/\d/.test(e.detail.value.money)){
      wx.showToast({
        title: '请正确输入提现金额！',
        icon: 'none'
      });
      return false
    }
    if (Number(e.detail.value.money) > Number(this.data.bankData.commission)) {
      wx.showToast({
        title: '可用金额不足！',
        icon: 'none'
      });
      return false
    }
    if (Number(e.detail.value.money) < Number(this.data.bankData.com)){
      wx.showToast({
        title: '最小提现金额为' + this.data.bankData.com+'元',
        icon: 'none'
      });
      return false
    }
    if (this.data.isSetPass == 0) {
      wx.hideLoading();
      wx.showModal({
        title: '温馨提示',
        content: '你还未设置支付密码，去设置？',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/passRecoverySet/passRecoverySet',
            })
          }
        }
      });
      return false
    };
    wx.hideLoading();
    this.setData({
      money: e.detail.value.money
    });
    this.enterPay();
  },
  //余额支付弹窗--监听支付密码
  OnPassword(e) {
    this.setData({ password: e.detail.value })
  },
  enterPay() {
    const thta = this;
    $wuxKeyBoard.show({
      className: 'className',
      titleText: '安全键盘',
      cancelText: '取消',
      inputText: '输入数字密码',
      showCancel: true,
      disorder: false,
      callback(value) {
        wx.showLoading({title: '请稍后...',});
        wx.request({
          url: app.baseURL + 'Distribution&a=commission_request',
          data: {
            user_id: app.globalData.userId,
            card_id: thta.data.bankCardList.id,
            price: thta.data.money,
            pwd: value
          },
          success: (res) => {
            if (res.data.status == 1) {
              wx.hideLoading();
              wx.reLaunch({ url: "/pages/commentResult/commentResult?title=操作成功&tip=我们会在15个工作日内为你处理" });
            } else {
              wx.hideLoading();
              wx.showToast({ title: res.data.info, icon: 'none' })
            }
          },
          fail: (res) => {
            wx.hideLoading();
            wx.showToast({ title: '网络错误', icon: 'none' })
          }
        })
      },
    })  
  },

  //添加银行卡
  toAddBank(){
    wx.navigateTo({
      url: '../addBankCard/addBankCard',
    })
  }

})